Ext.define('Material', {
	extend : 'Ext.data.Model',
	fields : [ 'id', 'name', 'altNames', 'category' ]
});

Ext.define('MaterialModel', {
	extend : 'Ext.data.Model',
	fields : [ 'id', 'materialId', 'model', 'code', 'unit', 'referenceCode',
			'note' ]
});

(function(material) {
	Ext.define('JX.Material', material);
})
		({
			singleton : true,

			modelSearchUrl : false,
			materialSearchUrl : false,
			materialCreateUrl : false,
			modelCreateUrl : false,
			materialModelManageUrl : false,

			chooseModel : function(callback, scope) {
				var win, filter, materialGrid, materialStore, modelGrid, modelStore, selectButton, manageModelButton;
				selectButton = Ext.widget('button', {
					text : '选择',
					disabled : true,
					selectedModel : null,
					iconCls : 'icon-accept',
					handler : function(button) {
						if (button.selectedMaterial) {
							win.close();
							callback.call(scope || this,
									button.selectedMaterial,
									button.selectedModel);
						}
					}
				});
				var filterTriggerClick = function(trigger) {
					materialStore.load({
						params : {
							query : filter.getValue()
						}
					});
				};
				filter = Ext.widget('trigger', {
					'triggerCls' : 'x-form-search-trigger',
					'onTriggerClick' : filterTriggerClick,
					width : 290,
					'listeners' : {
						specialkey : function(field, e) {
							if (e.getKey() == e.ENTER) {
								filterTriggerClick();
							}
						}
					}
				});
				materialStore = new Ext.data.Store({
					model : 'Material',
					pageSize : 20,
					proxy : {
						type : 'ajax',
						url : this.materialSearchUrl,
						reader : {
							type : 'json',
							root : 'data'
						}
					}
				});
				modelStore = new Ext.data.Store({
					model : 'MaterialModel',
					pageSize : 20,
					proxy : {
						type : 'ajax',
						url : this.modelSearchUrl,
						reader : {
							type : 'json',
							root : 'data'
						}
					}
				});
				materialStore.on('beforeload', function(store, operation,
						options) {
					Ext.apply(store.proxy.extraParams, {
						query : filter.getValue()
					});
				});
				var modelStoreBeforeload = function(store, operation, options) {
					var selection = materialGrid.getSelectionModel()
							.getSelection();
					var materialId = selection && selection[0] ? selection[0]
							.get('id') : 0;
					Ext.apply(store.proxy.extraParams, {
						materialId : materialId
					});
					if (materialId > 0) {
						manageModelButton.setDisabled(false);
					} else {
						manageModelButton.setDisabled(true);
					}
				};
				modelStore.on('beforeload', modelStoreBeforeload);
				materialGrid = Ext.widget('grid', {
					region : 'center',
					store : materialStore,
					columns : [ {
						header : '名称',
						dataIndex : 'name',
						sortable : false,
						flex : 1
					}, {
						header : '其它名称',
						dataIndex : 'altNames',
						sortable : false,
						flex : 1
					}, {
						header : '类别',
						dataIndex : 'category',
						sortable : false,
						flex : 1
					} ],
					dockedItems : [ {
						xtype : 'pagingtoolbar',
						store : materialStore, // same store GridPanel
						// is using
						dock : 'bottom',
						displayInfo : true
					}, {
						xtype : 'toolbar',
						dock : 'top',
						items : [ filter, '->', {
							xtype : 'button',
							text : '新建',
							iconCls : 'icon-cog-add',
							handler : function() {
								window.open(JX.Material.materialCreateUrl);
							}
						} ],
					} ],
				});
				var manageModelFunction = function() {
					var selection = materialGrid.getSelectionModel()
							.getSelection();
					var materialId = selection && selection[0] ? selection[0]
							.get('id') : 0;
					window.open(JX.Material.materialModelManageUrl.replace(
							'__MaterialId__', materialId));
				};
				manageModelButton = Ext.widget('button', {
					text : '编辑型号',
					disabled : true,
					iconCls : 'icon-cog-edit',
					handler : manageModelFunction
				});
				modelGrid = Ext.widget('grid', {
					region : 'center',
					store : modelStore,
					columns : [ {
						header : '型号',
						dataIndex : 'model',
						sortable : false,
						flex : 1
					}, {
						header : '单位',
						dataIndex : 'unit',
						sortable : false,
						flex : 1
					}, {
						header : '清单代码',
						dataIndex : 'code',
						sortable : false,
						flex : 1
					}, {
						header : '类别',
						dataIndex : 'referenceCode',
						sortable : false,
						flex : 1
					}, {
						header : '备注',
						dataIndex : 'note',
						sortable : false,
						flex : 1
					} ],
					dockedItems : [ {
						xtype : 'toolbar',
						items : [ manageModelButton ]
					}, {
						xtype : 'pagingtoolbar',
						store : modelStore, // same store GridPanel is
						// using
						dock : 'bottom',
						displayInfo : true
					} ],
				});

				materialGrid.on('selectionchange', function(view, selections,
						options) {
					modelStore.load();
				});
				var modelGridSelectionChange = function(view, selections,
						options) {
					if (selections.length == 1) {
						selectButton.selectedMaterial = materialGrid
								.getSelectionModel().getSelection()[0].raw;
						selectButton.selectedModel = selections[0].raw;
						selectButton.setDisabled(false);
					} else {
						selectButton.selectedMaterial = false;
						selectButton.selectedModel = false;
						selectButton.setDisabled(true);
					}
				};
				modelGrid.on('selectionchange', modelGridSelectionChange);

				win = Ext.widget('window', {
					title : '选择物品型号',
					width : 780,
					height : 480,
					modal : true,
					layout : 'border',
					closable : false,
					buttons : [ selectButton, {
						text : '清除',
						iconCls : 'icon-brick-delete',
						handler : function() {
							win.close();
							callback.call(scope || this, '');
						}
					}, {
						text : '取消',
						iconCls : 'icon-cancel',
						handler : function() {
							win.close();
							// callback.call(scope || this, []);
						}
					} ],
					items : [ {
						region : 'west',
						title : '物品名称',
						xtype : 'panel',
						width : 380,
						split : true,
						layout : 'fit',
						collapsible : false,
						floatable : false,
						// dockedItems : [ filter ],
						items : [ materialGrid ]
					}, {
						region : 'center',
						layout : 'fit',
						items : [ modelGrid ]
					} ]
				});
				win.show();
				materialStore.load();
			},
			updateMaterialRequisitionHistoryUrl : null,
			updateMaterialRequisitionHistory : function(modelId, containerId,
					lastRequisitionContainerId, params) {
				if (!modelId) {
					return;
				}
				jQuery('#' + containerId).html('正在更新..');
				jQuery.get(JX.Material.updateMaterialRequisitionHistoryUrl, Ext
						.apply({
							materialModelId : modelId
						}, params), function(data) {
					jQuery('#' + containerId).html(data.history);
					jQuery('#' + lastRequisitionContainerId).html(data.last);
				}, 'json');
			},
			updateMaterialWarehouseInboundHistoryUrl : null,
			updateMaterialWarehouseInboundHistory : function(modelId,
					historyContainerId, lastContainerId, params) {
				if (!modelId) {
					return;
				}
				jQuery('#' + historyContainerId).html('正在更新..');
				jQuery
						.get(
								JX.Material.updateMaterialWarehouseInboundHistoryUrl,
								Ext.apply({
									materialModelId : modelId
								}, params), function(data) {
									jQuery('#' + historyContainerId).html(
											data.history);
									jQuery('#' + lastContainerId).html(
											data.last);
								}, 'json');
			},
			updateMaterialReceiptHistoryUrl : null,
			updateMaterialReceiptHistory : function(modelId,
					historyContainerId, lastContainerId, params) {
				if (!modelId) {
					return;
				}
				jQuery('#' + historyContainerId).html('正在更新..');
				jQuery.get(JX.Material.updateMaterialReceiptHistoryUrl, Ext
						.apply({
							materialModelId : modelId
						}, params), function(data) {
					jQuery('#' + historyContainerId).html(data.history);
					jQuery('#' + lastContainerId).html(data.last);
				}, 'json');
			}
		});